package co2.bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.engine.ExceptionCo2;
import co2.pojos.MstUser;

@ManagedBean
@SessionScoped
public class SignInBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private String userId;
	private String username;
	private String password;
	private String uuid;
	private MstUser theUser;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	private Map map;
	
	@PostConstruct
	public void postCon(){
		map = new HashMap();
	}
	
	public void doLogin(){
		try{
			if(checkLogin(userId,password)){
				String extUrl=FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
				
				try {
					if(theUser.getRole()!=null && theUser.getRole().equals("ADMIN")){
						FacesContext.getCurrentInstance().getExternalContext().redirect(extUrl+"/jsf/target/daftar_user.jsf");
					}else{
						FacesContext.getCurrentInstance().getExternalContext().redirect(extUrl+"/jsf/target/user_material.jsf");
					}
						
					
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}else{
				 
				FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"ERROR!", "Username dan/atau password anda salah!"));
			 
			
			}
		}catch(ExceptionCo2 coe){
			coe.printStackTrace();
			FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Login: ","User belum aktivasi"));
		}
		
	}
	
	public boolean checkLogin(String userId, String password) throws ExceptionCo2{
		boolean status = false;
		theUser = (MstUser) dao.get(MstUser.class, userId);
		
		if(theUser!=null && theUser.getPassword().equals(password)){
			status = true;
			Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			sessionMap.put("theUser123", theUser);
			if(theUser.getStatus() == null){
				throw new ExceptionCo2("user innactive");
			}
		}
		
		return status;
	}
	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public MstUser getTheUser() {
		return theUser;
	}

	public void setTheUser(MstUser theUser) {
		this.theUser = theUser;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}
	
}
